package com.chj.redis.lock;

import redis.clients.jedis.Jedis;

/**
 * jedis锁
 * 
 * @author admin
 * @version [版本号, 2017年8月15日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JedisLock extends RedisLock {

	private final Jedis jedis;

	public JedisLock(Jedis jedis, String lockKey, int retryTimes, int expireMsecs) {
		super(lockKey, retryTimes, expireMsecs);
		this.jedis = jedis;
	}

	@Override
	protected void deleteKey() {
		jedis.del(getLockKey());
	}

	@Override
	protected Long get() {
		String str = jedis.get(getLockKey());
		Long value = null;
		if (str != null && str.length() > 0) {
			value = new Long(str);
		}
		return value;
	}

	@Override
	protected Boolean setNx(Long value) {
		Long result = jedis.setnx(getLockKey(), value.toString());
		return !(result == null || result == 0);
	}

	@Override
	protected Long getAndSet(Long value) {
		String str = jedis.getSet(getLockKey(), value.toString());
		Long result = null;
		if (str != null && str.length() > 0) {
			result = new Long(str);
		}
		return result;
	}

}
